package com.tuniu.agents.impl.trip;

import com.tuniu.agents.agent.AgentManagerGroup;
import com.tuniu.agents.agent.AgentOptions;
import com.tuniu.agents.agent.OptionsAgent;
import com.tuniu.agents.common.handler.IStreamResponseHandler;
import com.tuniu.agents.common.message.BaseAgentNewResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.messages.Message;
import reactor.core.publisher.Flux;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class OnlineSearcher extends OptionsAgent {
    public static final String AGENT_ID = "ONLINE_SEARCHER";
    private final String vectorMatchTargetUrl;
    private final Map<String, String> headers;
    private final IStreamResponseHandler streamResponseHandler;

    public OnlineSearcher(AgentManagerGroup agentManagerGroup,
                           String agentId,
                           String promptTemplatedId,
                           String vectorMatchTargetUrl,
                           IStreamResponseHandler streamResponseHandler) {
        this(agentManagerGroup, AGENT_ID, AGENT_ID, vectorMatchTargetUrl, new HashMap<>(), streamResponseHandler);
    }
    public OnlineSearcher(AgentManagerGroup agentManagerGroup,
                           String agentId,
                           String promptTemplatedId,
                           String vectorMatchTargetUrl,
                           Map<String, String> headers,
                           IStreamResponseHandler streamResponseHandler) {
        super(agentManagerGroup, new AgentOptions(agentId, promptTemplatedId));
        this.vectorMatchTargetUrl = vectorMatchTargetUrl;
        this.headers = headers;
        this.streamResponseHandler = streamResponseHandler;
    }
    @Override
    public Flux<String> stream(String conversationId, List<Message> messages, Map<String, Object> context) {
        log.info("Search Agent is processing conversation data: {}", conversationId);
        try {
            String message = messages.get(0).getText();
            Map<String, Object> handlerContext = new HashMap<>();
            handlerContext.put(SearchStreamResponseHandler.CONVERSATION_ID, conversationId);
            handlerContext.put(SearchStreamResponseHandler.VECTOR_MATCH_URL, vectorMatchTargetUrl);
            handlerContext.put(SearchStreamResponseHandler.HEADERS, headers);
            handlerContext.put(SearchStreamResponseHandler.TRIP_SUMMARY, message);
            return streamResponseHandler.handle(handlerContext, Flux.empty());
        } catch (Exception e) {
            log.error("Search Agent: Error processing trip summary");
            return Flux.just(BaseAgentNewResp.sendAnswerMsg(null, "Failed to process trip summary: " + e.getMessage()));
        }
    }

    @Override
    public String call(String conversationId, List<Message> messages, Map<String, Object> context) {
        log.info("Search Agent is processing conversation data: {}", conversationId);
        try {
            String message = messages.get(0).getText();
            Map<String, Object> handlerContext = new HashMap<>();
            handlerContext.put(SearchStreamResponseHandler.CONVERSATION_ID, conversationId);
            handlerContext.put(SearchStreamResponseHandler.VECTOR_MATCH_URL, vectorMatchTargetUrl);
            handlerContext.put(SearchStreamResponseHandler.HEADERS, headers);
            handlerContext.put(SearchStreamResponseHandler.TRIP_SUMMARY, message);
            return streamResponseHandler.handle(handlerContext, Flux.empty()).blockFirst();
        } catch (Exception e) {
            log.error("Search Agent: Error processing trip summary");
            return Flux.just(BaseAgentNewResp.sendAnswerMsg(null, "Failed to process trip summary: " + e.getMessage())).blockFirst();
        }
    }
}
